<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;
use DB;
class ForumController extends Controller
{
    //
    public function __construct()
    {
        $this -> middleware('login');
    }

    public function add()
    { 
       return view('admin.forum.add',['title'=>'添加留言']);
    }

    public function insert(Request $request)
    {
    	
    	$data = $request -> except('_token');
    	$data['uid'] = Session('master') -> id;
    	$time = Date('Y-m-d H-i-s');
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        // dd($data);
    	$res = DB::table('forum') -> insert($data);
    	if($res)
    	{
    		return redirect('admin/forum/index') -> with(['info' => '添加成功']);
    	}else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}	
    }

    public function index(Request $request)
    {
    	$num = $request -> input('num',10);
        $keywords = $request -> input('keywords','');
        $data = DB::table('forum') -> where('fmpost','like','%'.$keywords.'%') -> orderby('created_at','desc') -> paginate($num) ;
        // dd($data);
        foreach($data as $row)
        {
        	$row -> uid = DB::table('users') -> where('id', $row -> uid) -> first() -> name;
            $row -> fmpost = substr($row -> fmpost, 0 , 20 )."...";
        }


        // dd($data);
        return view('admin.forum.index',['data' => $data ,'request' => $request -> all(), 'title' => '留言列表']);
    }

    public function edit($id)
    {
    	$data = DB::table('forum') -> where('id', $id) -> first();
    	return view('admin.forum.edit',['title'=>'编辑留言', 'data' => $data]);
    }

    public function update(Request $request)
    {
    	$data = $request -> except('_token', 'id');
    	$id = $request -> id;
    	// dd($id);
    	$res = DB::table('forum') -> where('id', $id) -> update($data);
    	if($res)
    	{
    		return redirect('admin/forum/index') -> with(['info' => '修改成功']);
    	}else
    	{
    		return back() -> with(['info' => '修改失败']);
    	}
    }

    public function delete($id)
    {   
        DB::beginTransaction();
    	$res = DB::table('forum') -> where('id',$id) -> delete();
        if(DB::table('lpinglun') -> where('fid', $id)->first())
        {
            $rr = DB::table('lpinglun') -> where('fid', $id) -> delete();
            if(!$rr)
            {   
                DB::rollback();
                return back() -> with(['info' => '删除失败']);
            }
        }
        
    	if($res)
    	{   
            DB::commit();
    		return redirect('admin/forum/index') -> with(['info' => '删除成功']);
    	}else
    	{
            DB::rollback();
    		return back() -> with(['info' => '删除失败']);
    	}
    }

    public function detail($id)
    {
            
            $data = DB::table('forum') -> where('id', $id) -> first();
            
            $data -> upic = DB::table('users_info') -> where('user_id', $data -> uid) -> first() -> pic;
            $data -> uid = DB::table('users') -> where('id', $data -> uid) -> first();
            $pldata = DB::table('lpinglun') -> where('fid', $id) -> get();
            
            foreach($pldata as $row)
            {
                $row -> upic = '';
                $row -> uname = '';
                $row -> upic = DB::table('users_info') -> where('user_id', $row -> uid) -> first()-> pic;
                $row -> uname = DB::table('users') -> where('id', $row -> uid) -> first() -> name;    
            }
            // dd($data);
            return view('admin.forum.detail',['title'=>'留言详情', 'data' => $data, 'pldata' => $pldata]);
    }

    public function pldel($id)
    {
        $res = DB::table('lpinglun') -> where('id',$id) -> delete();
        if($res)
        {
            return back() -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }
    }
}
